/**
 * 产品接口
 */
var express = require('express');
var router = express.Router();

var sql = require('../db/index');
/**
@swagger
 *       /employee/saveOrUpdate:
 *       post:
 *           description: 用户新增或者修改接口
 *           tags: [员工]
 *           produces:
 *               - application/x-www-form-urlencoded
 *           parameters:
 *               - name: employee_id
 *                 description: 员工ID
 *                 required: true
 *                 type: number
 *               - name: username
 *                 description: 员工名
 *                 required: true
 *                 type: string
 *               - name: email
 *                 description: 员工邮箱
 *                 required: true
 *                 type: string
 *               - name: password
 *                 description: 员工密码
 *                 required: true
 *                 type: string
 *               - name: avatar
 *                 description: 员工头像
 *                 required: false
 *                 type: string
 *               - name: role_id
 *                 description: 员工角色ID
 *                 required: false
 *                 type: number
 *           responses:
 *               200:
 *                   description: 操作成功
 *               500:
 *                   description: 服务器错误

 */

//新增或者修改接口
router.post('/saveOrUpdate', async (req, res) => {
    // 从请求体中获取参数
    const { id, name, email, role } = req.body;

    // 根据 id 的有无执行不同的操作
    let mysqlQuery = '';
    if (id) {
        // 如果存在 id，则执行更新操作
        mysqlQuery = `UPDATE users SET name='${name}', email='${email}', role='${role}' WHERE id=${id}`;
    } else {
        // 如果不存在 id，则执行插入操作
        mysqlQuery = `INSERT INTO users (name, email, role) VALUES ('${name}', '${email}', '${role}')`;
    }

    // 执行数据库操作，此处省略具体代码
    // await sql.query(mysqlQuery);

    // 响应给前端
    res.send({
        status: 200,
        message: "操作成功",
        data: null,
        timestamp: new Date().getTime()
    });
})
module.exports = router;